import { Controller, Get, Query } from '@midwayjs/core';
import { Inject } from '@midwayjs/decorator';
import { Context } from 'koa';
import { WeatherInfo } from '../interface';
import { WeatherService } from '../service/weather.service';

@Controller('/')
export class WeatherController {

  @Inject()
  ctx: Context;

  @Inject()
  weatherService: WeatherService;

  @Get('/weather')
  async getWeatherJson(@Query('id') cityId: string): Promise<WeatherInfo> {
    return this.weatherService.getWeather(cityId);
  }

  @Get('/weatherHtml')
  async getWeatherHtml(@Query('cityId') cityId: string): Promise<void> {
    const result = await this.weatherService.getWeather(cityId);
    if (result) {
      await this.ctx.render('info', result.weatherinfo);
    }
  }
}